package chapter3to7.src.main.java.com.kaikeba.concurrent;

import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author 吴世凯
 * 邮箱：
 * 日期：3/21/2021
 */
public class TestThreadPool {


    public static void main(String[] args) throws InterruptedException {




        long start = System.currentTimeMillis();

        final Random random = new Random();


        final ArrayList<Integer> list = new ArrayList<>();
        ExecutorService executorService = Executors.newSingleThreadExecutor();

        for (int i = 0; i < 100000; i++) {

            executorService.execute(
                    new Runnable() {
                        @Override
                        public void run() {
                            list.add(random.nextInt());
                        }
                    }
            );

        }


        executorService.shutdown();
        boolean b = executorService.awaitTermination(1, TimeUnit.DAYS);
        System.out.println("time = " + ( System.currentTimeMillis()- start));
        System.out.println("list.size() = " + list.size());


    }


}
/*
*
* 使用线程池
* time = 56
list.size() = 100000
* */